var data = require("./data.js");
// console.log(data);
//获取treedata中name的对应记录{}
function getDataByName(name, treedata) {
    var result = {};
    var isFind = false;
    for (item of treedata) {
        var sum = 0;
        for (department of item.children) {
            for (group of department.children) {
                for (p of group.children) {
                    if (p.name == name) {
                        result.company = item.company;
                        result.name = p.name;
                        result.sales = p.sales;
                        isFind = true;
                    }
                    sum += p.sales
                }
            }
        }
        if (isFind) {
            result.sum = sum;
            break;
        }
    }
    return result;
}

console.log(getDataByName("人员520", data.treedata));

//
function getMaxSale(listdata) {
    var maxarr = [];//输出数组
    for (p of listdata) {
        var isPush=true;//假定需要把p元素放到maxarr中
        for(var index=0;index<maxarr.length;index++){//遍历maxarr
            if(maxarr[index].name==p.name){
                //当max中已经错在相同名字时，分下面两种情况
                if(p.sales>maxarr[index].sales){//sales大于maxarr中的，删除maxarr中的当前数据
                    maxarr.splice(index,1);
                }else{
                    isPush=false;     //max中的sale大，不添加 让isPush变为false              
                }
                break;
            }
        }
        if(isPush){//根据isPush 判断是否要把当前数据放入到maxarr中
            maxarr.push(p);
        }
    }
    return maxarr;
}
// console.log(data.listdata);
console.log(getMaxSale(data.listdata));